package test.temp;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.BasicCookieStore;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import utils.SmallTool;
import utils.httpclient.HttpClient;
import utils.httpclient.HttpClientConfig;
import utils.httpclient.HttpClientUtil;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/10/25 11:35
 */
public class Down1TestParseHtml3 {
    @SneakyThrows
    public static void main(String[] args) {


        // 进行抓取网页
        Db use = Db.use();
        List<Entity> query = use.query("select * from pmid_img");

        for (Entity entityParam : query) {
            if (!StringUtils.isNotEmpty(entityParam.getStr("html"))) {
                String pmid = entityParam.getStr("pmid");
                String baseUrl = "https://pubmed.ncbi.nlm.nih.gov";
                String url2 = "https://pubmed.ncbi.nlm.nih.gov/" + pmid + "/";
                BasicCookieStore cookieStore = new BasicCookieStore();
                HttpClientConfig config = HttpClientUtil.getSearConfig(url2, baseUrl, "", true);
                String s = "";
                try {
                    s = HttpClient.getHtml(cookieStore, config);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (StringUtils.isNotEmpty(s)) {
                    Entity entity = new Entity();
                    Entity where = new Entity();
                    where.set("pmid", pmid);
                    entity.setTableName("pmid_img");
                    entity.set("pmid", pmid);
                    entity.set("html", s);
                    try {
                        use.update(entity, where);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                }
            }

        }


        int exist = 0;
        for (Entity entity : query) {
            String pmid_html = entity.getStr("html");
            Integer pmid1 = entity.getInt("pmid");
            if (pmid1 == 36278474) {

                System.out.println("pmid1");
            }
            if (StringUtils.isNotEmpty(pmid_html)) {
                if (pmid_html.contains("item-list usa-unstyled-list")) {
                    exist++;
                    Document parse = Jsoup.parse(pmid_html);
                    Element element = parse.selectFirst("ul.item-list.usa-unstyled-list");
                    if (element != null) {
                        try {
                            String title = element.selectFirst("a").text();
                            String pmid = element.selectFirst("a").attr("href").replace("/", "");
                            Entity where = new Entity();
                            where.setTableName("pmid_html");
                            where.set("pmid", pmid1);
                            entity.set("Retractionofpmid", pmid);
                            entity.set("Retractionoftitle", title);
                            use.update(entity, where);
                        } catch (Exception e) {

                        }

                    }
                } else {

                }

            }
        }
        SmallTool.printMess("exist+++" + exist);


    }
}
